package com.bookmngsys.config;

import com.alibaba.fastjson.JSON;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.bookmngsys.entity.AliPayBean;
import lombok.Data;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;

@Data
@Configuration
public class AlipayConfig {

    /**日志对象*/
    private static final Logger logger = LoggerFactory.getLogger(AlipayConfig.class);

    private final String format = "json";

    /**
     * appId
     */
    @Value("2021000119683176")
    private String appId;

    /**
     * 商户私钥
     */
    @Value("MIIEvwIBADANBgkqhkiG9w0BAQEFAASCBKkwggSlAgEAAoIBAQDBaUPvFUX4LhRol/jVjJzWQSNtm/0kXMEEDlBIYZlKT+SWM1qrA4PcW5oKSbm9E8rcaH5lSGgef7pYMkvm2/F8/4ggc8fEGZpItUq3nhlWHPAPzUao7do71mHv/DIb0wgYGzbytOp8vK6Q69dxwQ5g2lMRVP56vxDZ7jevbXL6EQ1qQLGb4rENONQG40EpjccH2ZORTi41t4DSyAn5ZGphU1G94YR1zDF7Bx56Z0DJOULC15yRPJC2wHLMxwKdd9Zhycj3lY7RJUHIZKQ+0dOkoddotSRDOmsFLwZ7ON+yk2er9SCsOl2DqC3bnFveAD3tTwic/CWJ31E8f0fAJZWdAgMBAAECggEBALJkhWRgGslutyRCCCHwMDjum97dof+IDf/Ps/0PCHSifBSOCXLzKwx08P/MQKVFPDdPqkWBI4f+B8KRobCt7W99wfEs2XTmqIenr+Ls0DG9Py9UB3iI6mGGu46+7vj7DNwvPd9fP2kOH3VMEsu5Zamvti6mVpKg6MoaDwKf0Q0UUeHgr3WO5IhUNK3EwkVY5pdjXJVKIapT3i6d63lHyd06u6UPKvAYPDlUp1aSYaFqLOFdjifYb3r/7rAAXxw0jZPOWSPjqzihrMlPTQRfKzZo0VJqp/Q+xoMrEkLyJ2EvuNacG5ycNYc19OpxHnjHZxYJPV4iPiALxU3Gz8XaEAECgYEA7w9xEWLtCTjEZH5XjADIOLaBUZOGQl+z1ZV0d9NDXyxxjuTCTTvFEGnnnme+kDq6yTrwso1VcTsq9kZRt93qNXR9jK6DjKcZ8J/DQaq/mGcW2IxlRpQBs6ap3mIY5XIT/2IGxDR3R2UMhTOiu/39EBSogOo1BDHApx1BJhq+q+ECgYEAzx2/bOa0fqsmy2iXKLdcVzog4WVKd3jR5YDVJ4fk2+Np/YegtNuxBVibXTJDFOToHvQjspBpH/FJavm5H81tvVneIyMAmQPodw7l3UcLHkhvc7NEGXQ9wLZDeTdT1qnV5TZ2e6soyUOxOR7NRXC5dLwfMo9F28/g+sjdPnViwT0CgYEAgFhI+u2ilu3cOpu+AtM0q4YIPbS3QxFAam3X3rEUnZctzGqyimaJcTt6d4f5PlPZjknPifzpH2aFtTyhiNZidJp7ATl8NNpNyersGGhEftzx3gbaJJ83nqzezB7cgOrTI6wMN/B3DWSJ6UWEm+tLnwOIR9X16q+PX+kL3hpDveECgYAoE2B76PQZ4nIiP72n8jra4fOEyX2sOmOa3d/gw0nERm+2z+yHsk7BcxC06snSHCzR+HmDzq+Yr65+FY39mllWnzH9aINwGDJ9/xL5SwratDdkktZlenmMdXf+13bQfZJWxMAOU+MTdpavL8eWXSZ1cQU0WkaxLrsgq4cr07jlHQKBgQCw9TbygNOSEoqHZa5oyg9gdalFk95GWyouwRnB9/kU/b6rk+XqgLSohk6+ejG0Io+v3Gr1OuLfdLyfx4wOdmUP8obr1wX8rv1oWnk/3/2gMr7HqudubpF4B3O5ywqvjcGbrNStJuZpBFqmBzLASzcRdEw9eyV3dGVZzQ7dla4RpA==")
    private String privateKey;

    /**
     * 支付宝公钥
     */
    @Value("MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAtaXdSItyboj5uLdIe3x1kX4cCz+h2jYYLnyjC+UH/9y8IwU9xXtIiXlBb61HMEM+GQvlmtJvTWQ8SGUgm/48ydD2Ayf+/wA4FDYmk0quzZYfi8+MY7sgvR1hvezzit2ZasHvqgSawDXLz2Ko2wgU2SE28QaOxo98fllOB/mLT5eXcbPT+Wvj3stCbyzMpAfKrS1DlX3neIdgmyNwpXQVff3ClFZdTJD2BM7m5e6CVS+UV7jfnJtRls/yHdBUQAoobiwf/BPPa4LRVy2BN/HO4CXwO54kR3WtQAfueUBB1b2/v5QGR9HTiwOTsNPEiYSU+V0QwDEYWnOj91pRmpUv3wIDAQAB")
    private String publicKey;

    /**
     * 服务器异步通知页面路径，需要公网能访问到
     */
    @Value("http://localhost:8080/api/book/payMoney")
    private String notifyUrl;

    /**http://localhost:8080/api/book/payMoney
     * http://localhost:8080/barcode/return_url.html
     * 服务器同步通知页面路径，填写自己的成功页面路径
     */
    @Value("http://localhost:8080/api/book/payMoney")
    private String returnUrl;

    /**
     * 签名方式
     */
    @Value("RSA2")
    private String signType;

    /**
     * 字符编码格式
     */
    @Value("utf-8")
    private String charset;

    /**
     * 支付宝网关
     */
    @Value("https://openapi.alipaydev.com/gateway.do")
    private String gatewayUrl;

    public String pay(AliPayBean aliPayBean) throws AlipayApiException {

        AlipayClient alipayClient = new DefaultAlipayClient(
                gatewayUrl, appId, privateKey, format, charset, publicKey, signType);

        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(returnUrl);
        alipayRequest.setNotifyUrl(notifyUrl);
        alipayRequest.setBizContent(JSON.toJSONString(aliPayBean));
        logger.info("封装请求支付宝付款参数为:{}", JSON.toJSONString(alipayRequest));

        String result = alipayClient.pageExecute(alipayRequest).getBody();
        logger.info("请求支付宝付款返回参数为:{}", result);

        return result;
    }
}

